package com.flower.subject.application.controller;


import com.alibaba.fastjson.JSON;
import com.flower.subject.application.convert.SubjectAnswerDTOConverter;
import com.flower.subject.application.convert.SubjectCategoryDTOConverter;
import com.flower.subject.application.convert.SubjectInfoDTOConverter;
import com.flower.subject.application.dto.SubjectCategoryDTO;
import com.flower.subject.application.dto.SubjectInfoDTO;
import com.flower.subject.domain.convent.SubjectInfoConverter;
import com.flower.subject.domain.entity.SubjectAnswerBO;
import com.flower.subject.domain.entity.SubjectCategoryBO;
import com.flower.subject.domain.entity.SubjectInfoBO;
import com.flower.subject.domain.service.SubjectCategoryDomainService;
import com.flower.subject.domain.service.SubjectInfoDomainService;
import com.flower.subject.entity.PageResult;
import com.flower.subject.entity.Result;
import com.flower.subject.infra.basic.entity.SubjectCategory;
import com.flower.subject.infra.basic.entity.SubjectInfoEs;
import com.flower.subject.infra.basic.service.SubjectEsService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-02 18:06
 */
@RestController
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @Resource
    private SubjectEsService service;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        if(log.isInfoEnabled()){
            log.info("dto:{}", JSON.toJSONString(subjectInfoDTO));
        }
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANT.convertDTOtoBO(subjectInfoDTO);
        List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANT.convertListDTOtoBO(subjectInfoDTO.getOptionList());
        subjectInfoBO.setOptionList(subjectAnswerBOS);

        subjectInfoDomainService.add(subjectInfoBO);


        return Result.ok(true);
    }


    /**
     * 全文检索
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        SubjectInfoEs subjectInfoEs = new SubjectInfoEs();
        subjectInfoEs.setKeyWord(subjectInfoDTO.getKeyWord());
        PageResult<SubjectInfoEs> subjectInfoEsPageResult = service.querySubjectList(subjectInfoEs);


        return Result.ok(subjectInfoEsPageResult);
    }

    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANT.convertDTOtoBO(subjectInfoDTO);
        PageResult<SubjectInfoBO> subjectPage = subjectInfoDomainService.getSubjectPage(subjectInfoBO);


        return Result.ok(subjectPage);

    }


    /**
     * 查询项目信息
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANT.convertDTOtoBO(subjectInfoDTO);

        SubjectInfoBO subjectInfoBo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);

        SubjectInfoDTO dto = SubjectInfoDTOConverter.INSTANT.convertBOtoDTO(subjectInfoBo);


        return Result.ok(dto);

    }


    /**
     * 获取题目贡献榜
     * @param
     * @return
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {

        List<SubjectInfoBO> boList = subjectInfoDomainService.getContributeList();
        List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.INSTANT.convertBOListtoDTOList(boList);

        return Result.ok(dtoList);


    }






}
